
var LOCALHOST_ADDRESS = "http://39.107.45.130/";

var commonHeadImg = "images/ico/homeicon.png";

var userInfoObject = {};

//获得全部的分类信息。
$(function () {
    generateMenuItem();
    getUserInfoObject();
    addAnimation();
    bindRegersiter();
});

function bindRegersiter() {
    $("#register").click(function () {
        // layer.msg("暂未开放注册，请登录！");
        alert("暂未开放注册，请登录！");
    })
}

function getUserInfoObject() {
    //这里的userInfo是json的字符串；
    var userInfoJsonStr = $("#bindUserInfo").val();
    if (userInfoJsonStr == "null" || userInfoJsonStr == null || userInfoJsonStr == ""){
        $(".NoUserInfo").show();
        $("#userInfoLi").hide();
    }else {
        userInfoObject = JSON.parse(userInfoJsonStr);
        $(".NoUserInfo").hide();
        $("#userInfoLi").show();
        $("#iconImage")[0].src = (userInfoObject.headImg == null ? commonHeadImg : userInfoObject.headImg);
        // Text变迁对应com元素的innerText，相对应的还有innerHtml
        $("#userName").text(userInfoObject.nickName == null ? "攀小伙" : userInfoObject.nickName);
    }
}

function addAnimation() {
    $(".wrap>ul>li ul").hide();
    //获取.wrap下面的ul中的li,注册鼠标进入和离开的事件
    $('#nav li').hover(function() {
        $('ul', this).slideDown(400);
        // $(this).children('a:first').addClass("hov");
    }, function() {
        $('ul', this).slideUp(400);
        // $(this).children('a:first').removeClass("hov");
    });
}

function generateMenuItem() {
    $.ajax({
        "url":"get/all/classification",
        "type":"post",
        "contentType":"application/x-www-form-urlencoded",
        // 预期服务器返回的数据
        "dataType":"json",
        "error":function(response){
            var message = response.responseJSON.message;
            layer.msg(message);
        },
        "success":function(response){

            //获取分页数据,并且进行显示；
            var data = response.data;

            //生成表格数据,因为是异步请求,所以生成表格的代码必须写在success的回调函数中;
            generateMenuTable(data);
        }
    });
}



function generateMenuTable(data) {
    //生成下拉菜单的效果！
    var typeData = data.routeClassifications;
    $.each(typeData,function (index,value) {
        var Html = "<li><a href='#' num = '"+value.id+"' name = 'routeClassifications' class='myNavBat'>"+value.categorizationInformation+"</a></li>"
        $("#routeClassifications").append(Html);
    });

    var typeData = data.sceneryClassifications;
    $.each(typeData,function (index,value) {
        var Html = "<li><a href='#' num = '"+value.id+"' name = 'sceneryClassifications' class='myNavBat'>"+value.categorizationInformation+"</a></li>"
        $("#sceneryClassifications").append(Html);
    });

    var typeData = data.foodClassifications;
    $.each(typeData,function (index,value) {
        var Html = "<li><a href='#' num = '"+value.id+"' name = 'foodClassifications' class='myNavBat'>"+value.categorizationInformation+"</a></li>"
        $("#foodClassifications").append(Html);
    });
    var typeData = data.hotelClassifications;
    $.each(typeData,function (index,value) {
        var Html = "<li><a href='#' num = '"+value.id+"' name = 'hotelClassifications' class='myNavBat'>"+value.categorizationInformation+"</a></li>"
        $("#hotelClassifications").append(Html);
    });

    //绑定单击响应，在页面上方的单击响应，就是菜单项的单击函数；
    bindClassificationBtn();

}

function bindClassificationBtn() {
    $(".myNavBat").click(function () {
        //最大的那个分类，区别是景点，还是酒店，或者是路线等等。
        var bigClassificationName = $(this).attr("name");
        var classificationId = $(this).attr("num");
        //一定要有一个num的值对不对？嗯，没错，就这么干；因为包含在不同的页面，所以可以这样进行书写！！
        if (bigClassificationName == "sceneryClassifications") {

            window.location.href=LOCALHOST_ADDRESS+"hotSpots.html?classficationId="+classificationId+"";
        }else if (bigClassificationName == "foodClassifications") {

            window.location.href=LOCALHOST_ADDRESS+"specialFood.html?classficationId="+classificationId+"";
        }else if (bigClassificationName == "routeClassifications") {

            window.location.href=LOCALHOST_ADDRESS+"touristRoute.html?classficationId="+classificationId+"";
        }else {
            window.location.href=LOCALHOST_ADDRESS+"hotelReservation.html?classficationId="+classificationId+"";

        }
    });
}